package com.foryou.wechat.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.foryou.wechat.model.wx.Template;
import com.foryou.wechat.model.wx.TemplateParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <h1>推送用户订阅消息</h1>
 * @author LJX
 */
@Slf4j
@Service
public class WeChatService {

    private final RestTemplate restTemplate;

    public WeChatService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @Value("${wx.appId}")
    private String appId;

    @Value("${wx.appSecret}")
    private String appSecret;




    /**
     *
     * @param openid 用户openid
     * @param templateId 模板ID
     * @param params 模板参数LIST
     * @return
     */
    public String pushOneUser(String openid, String templateId, List<TemplateParam> params) {

        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccess_token();

        Template template=new Template();
        template.setTemplate_id(templateId);
        template.setTouser(openid);
        template.setPage("pages/index/index");
//        List<TemplateParam> paras=new ArrayList<>();
//        paras.add(new TemplateParam("amount1","000001"));
//        paras.add(new TemplateParam("date2","2019-5-8 10:10:10"));
//        paras.add(new TemplateParam("thing3","你分享的猫猫照片获得了爱心人士送的小鱼干"));
        template.setTemplateParamList(params);
        log.info(template.toJSON());
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, template.toJSON(), String.class);
        log.error("小程序推送结果={}", responseEntity.getBody());
        return responseEntity.getBody();
    }

    /**
     * 获取access_token
     * @return
     */
    public String getAccess_token() {
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> params = new HashMap<>(2);
        params.put("APPID", appId);
        params.put("APPSECRET", appSecret);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(
                "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}", String.class, params);
        String body = responseEntity.getBody();
        JSONObject object = JSON.parseObject(body);
        String Access_Token = object.getString("access_token");
        String expires_in = object.getString("expires_in");
        log.info("有效时长expires_in：" + expires_in);
        return Access_Token;
    }


//    public static void main(String[] args) {
//        System.out.println(new WeChatService().getAccess_token());
//
//        WeChatService weChatUtil = new WeChatService();
//        weChatUtil.pushOneUser(weChatUtil.getAccess_token(),"o3fiV5P9YJO2oD3-50PU6baV31j4","NhxFQQM83G2DMPBVQDFI0cObvBoF3oTZRYJNhnCnOz0");
//
//    }
}
